import java.util.Arrays;

/**
 * @author LKQ
 * @date 2022/3/4 9:23
 * @description 动态规划 F(S) 为组成面额 S所使用的最少硬币
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        int[] coins = {2};
        formalSolution.coinChange(coins, 3);
    }

    public int coinChange(int[] coins, int amount) {
        int n = coins.length;
        // 组成面额数的最少硬币
        int[] dp = new int[amount + 1];
        // 假设全部都由1块组成需要，amount + 1个硬币，如果最后值没有修改，说明不存在硬币组合
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < n; j++) {
                if (coins[j] <= i) {
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
    }
}